<?php

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 */
extract($_POST);

if($actionPlaylist == 'add') {
    $playlist_id = generatePlaylistID();
    $name_pl = stripslashes($name_pl);
    if($mysql->query("INSERT INTO ".$tb_prefix."playlist(playlist_id, playlist_user_id, playlist_name) VALUES ('$playlist_id', '$user_id', '$name_pl')")) {
        $rt['status'] = 'success';
        $rt['playlist_id'] = $playlist_id; 
        $rt['name_pl'] = $name_pl;
    } else {
        $rt['status'] = 'fail';
    }

} elseif ($actionPlaylist == 'change') {
    $playlist = m_get_data('PLAYLIST',$playlist_id);
    $playlist = trim($playlist,',');
    if ($playlist) {
        $$playlist_HTML = '';
        $q = $mysql->query("SELECT m_id, m_title, m_viewed, m_poster FROM ".$tb_prefix."data WHERE m_id IN (".$playlist.")");
        $i = 1;
        while ($r = $mysql->fetch_array($q)) {
                $song_ID 		= $r['m_id'];
                $song_TITLE 	= $r['m_title'];
                $user_ID 	= $r['m_poster'];
                $user_NAME1 =  m_get_data('USER',$user_ID);
                if($user_NAME1 == '')
                {
                        $user_NAME1 = 'DJ Other';
                }

                $user_URL	= url_link($user_ID,$user_NAME1,'User');	
                $m_viewed 	= $r['m_viewed'];	
                $song_TITLE1 = shortname($song_TITLE,40);				
                $song_URL		= url_link($song_ID,$song_TITLE,'nhac-san');
                
                $playlist_HTML .= "<li><p class='right'><span class='del'><a href=# onClick='removeFromPlaylist($song_ID);return false;' title='remove from Playlist'></a></span> </p> <p class='title'>$i. <a href='$song_URL' title='$song_TITLE'>$song_TITLE1</a></p><p class='user'>Đăng bởi: <a href='$user_URL'>$user_NAME1</a></p></li>";
                $i++;
        }
        $playlist_URL	= url_link($playlist_id,$user_name,'Playlist');		
        $rt['playAll'] = ($_SESSION['user_id'])?"<p style='margin-top:20px;margin-left:85px;' id='playAllPlaylist'> <a style='color:#89014b;' href=$playlist_URL><b>Nghe Playlist</b></a></p>":'';
        $rt['html'] = $playlist_HTML;
    } else {
        $rt['html'] = "<li><p class='title'>Playlist chưa có bài hát</a></p></li>";
        $rt['playAll'] = '';
    }
} elseif ($actionPlaylist == 'remove') {
    $r = $mysql->fetch_array($mysql->query("SELECT playlist_contents FROM ".$tb_prefix."playlist WHERE playlist_id='$playlist_id'"));
    $songlist = explode(',',$r['playlist_contents']);
    $playlist = $r['playlist_contents'];
    if(!in_array($song_id, $songlist)) {
        $rt['status'] = 'fail';
        $rt['msg'] = 'Bài hát không hợp lệ!';
        echo json_encode($rt);
        exit;
    } else {
        if ($playlist === $song_id) {
                $mysql->query("UPDATE ".$tb_prefix."playlist SET playlist_contents = ''  WHERE playlist_contents = '".$song_id."' AND playlist_id = '".$playlist_id."'");
                $rt['status'] = 'success';
        }
        else {
                $z = explode(',',$playlist);
                if (in_array($song_id,$z)) {
                        unset($z[array_search($song_id,$z)]);
                        $str = implode(',',$z);
                        if (!$str) {
                                $mysql->query("DELETE FROM ".$tb_prefix."playlist WHERE playlist_contents = '".$song_id."' AND playlist_id = '".$playlist_id."'");
                        }
                        else {
                                $mysql->query("UPDATE ".$tb_prefix."playlist SET playlist_contents = '".$str."' WHERE playlist_id = '".$playlist_id."'");
                        }
                        $rt['status'] = 'success';
                }
        }
    }
} elseif ($actionPlaylist == 'addSong') {
    $r = $mysql->fetch_array($mysql->query("SELECT playlist_contents FROM ".$tb_prefix."playlist WHERE playlist_id='$playlist_id'"));
    $playlist = $r['playlist_contents'];
    $z = explode(',',$playlist);
    if (!in_array($song_id,$z)) {
        if($playlist == '') {
            $playlist = $song_id;
        } else {
            array_push($z, $song_id);
            $playlist = implode(',', $z);
        }
            if($mysql->query("UPDATE ".$tb_prefix."playlist SET playlist_contents = '$playlist' WHERE playlist_id = '".$playlist_id."'")) {
                $rt['status'] = 'success';
            } else {
                $rt['status'] = 'fail';
                $rt['msg'] = 'Có lỗi xảy ra, vui lòng thử lại!';
            }
    } else {
        $rt['status'] = 'fail';
        $rt['msg'] = 'Bài hát đã có trong playlist!';
    }
} elseif ($actionPlaylist == 'removePlaylist') {
    if($mysql->query("DELETE FROM ".$tb_prefix."playlist WHERE playlist_id='$playlist_id'")) {
        $rt['status'] = 'success';
    } else {
        $rt['status'] = 'fail';
        $rt['msg'] = 'Có lỗi xảy ra, vui lòng thử lại!';
    }
}
echo json_encode($rt);

?>
